package com.jjwxc.jwjskandriod.my.adapter

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import com.jjwxc.jwjskandriod.R
import com.jjwxc.jwjskandriod.framework.imageloader.FFImageLoader
import com.jjwxc.jwjskandriod.framework.refresh.recyclerview.BaseRecyclerAdapter
import com.jjwxc.jwjskandriod.model.VIPBgResponse
import com.jjwxc.jwjskandriod.my.viewholder.PortraitViewHolder
import com.jjwxc.jwjskandriod.my.viewholder.PortraitViewHolder.Companion.from

class PortraitAdapter(private val type: Int) : BaseRecyclerAdapter<PortraitViewHolder>() {
    private val mData: MutableList<VIPBgResponse> = ArrayList()

    fun addData(data: List<VIPBgResponse>?) {
        if (data != null) {
            mData.addAll(data)
            notifyDataSetChanged()
        }
    }

    fun setData(data: List<VIPBgResponse>?) {
        this.mData.clear()
        addData(data)
    }

    /**
     * 創建ViewHolder
     */
    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int, isItem: Boolean): PortraitViewHolder = from(parent!!)
    override fun getViewHolder(view: View?): PortraitViewHolder = from(view as ViewGroup)

    /**
     * 綁定數據：可以直接拿到已經綁定控件的Viewholder對象
     */
    @SuppressLint("SetTextI18n", "ClickableViewAccessibility")
    override fun onBindViewHolder(
        holder: PortraitViewHolder,
        @SuppressLint("RecyclerView") position: Int,
        isItem: Boolean
    ) {
        FFImageLoader.loadImage(holder.itemView.context, holder.img_grid, mData[position].url)
        if (mData[position].isSelect) {
            holder.img_selct_one.visibility = View.VISIBLE
            holder.cl_grid_item.setBackgroundResource(R.drawable.vip_head_true)
        } else {
            holder.img_selct_one.visibility = View.INVISIBLE
            holder.cl_grid_item.setBackgroundResource(R.drawable.vip_head_false)
        }

        holder.img_selct_two.visibility = View.GONE

        holder.cl_grid_item.setOnClickListener {
            for (i in mData.indices) {
                mData[i].isSelect = i == position
            }
            notifyDataSetChanged()
        }
    }

    override fun getAdapterItemViewType(position: Int): Int = position

    override fun getAdapterItemCount(): Int = mData.size
}